<?php

namespace App\Http\Controllers\Wechat;


use App\Http\Controllers\MessagePacket;
use App\Model\Wechat\WechatConfig;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\News;
use EasyWeChat\Message\Text;



class HelpController
{
    use MessagePacket;

    protected $app; //应用实例
    protected $hosturl;


    public function __construct()
    {
        $wechat = WechatConfig::find(1);
        $options = [
            'debug'  => true,
            'app_id' => $wechat->appid,
            'secret' => $wechat->secret,
            'token'  => $wechat->token,
            'aes_key' => $wechat->aes_key, // 可选
            'log' => [
                'level' => 'debug',
                'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
            ],
            //...
        ];

        $this->app = new Application($options);
        $this->hosturl = $wechat->url;

    }

    public function textMessage($data)
    {
        $text = new Text();
        $text['content'] = $data;
        return $text;
    }

    public function newsMessage($data)
    {
        $news = new News();
        $news['title'] = $data['title'];
        $news['description'] = $data['description'];
        $news['url'] = $data['url'];
        $image = 'http://'.$_SERVER['HTTP_HOST'].$data['image'];
        $news['image'] = $image;
        return $news;
    }

}
